package com.gxa.autoparts.admin.controller;


import com.gxa.autoparts.admin.service.AdminService;
import com.gxa.autoparts.admin.service.PermissionService;
import com.gxa.autoparts.admin.service.RolePermissionService;
import com.gxa.autoparts.admin.service.RoleService;
import com.gxa.autoparts.admin.vo.RolePermissionVo;
import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.Admin;
import com.gxa.autoparts.core.pojo.Permission;
import com.gxa.autoparts.core.pojo.Role;
import com.gxa.autoparts.core.pojo.RolePermission;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/roleManage")
@RestController
@Transactional(rollbackFor = Throwable.class)
public class RoleManageController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("查询全部角色")
    @PostMapping("/findAllRole")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "页内数据条数", required = true)
    })
    public Map findAllRole(Integer page,Integer size){
        List<Role> roles = roleService.selectAllRole((page-1)*size, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(roles);
        jsonResult.put("totleNumber",roleService.selectAllRole2().size());
        jsonResult.setMsg("角色全查成功");
        return jsonResult.getDataMap();
    }
    @Autowired
    private RolePermissionService rolePermissionService;

    @ApiOperation("角色删除")
    @PostMapping("/deleteRole")
    @ApiImplicitParam(name = "id", value = "角色Id", required = true)
    public Map deleteRole(Long id) throws BusinessException {
        roleService.deleteById(id);
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRid(id);

        roleService.deleteById(id);
        roleService.delectByRid(id);

        /*List<RolePermission> select = rolePermissionService.select(rolePermission);
        if (select.size()==0){
            throw new BusinessException(ExceptionCode.WRONG_NO_PERMISSION);
        }
        for (RolePermission rolePermission1:select){
            rolePermissionService.deleteById(rolePermission1.getId());
        }*/
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("角色删除成功");
        return jsonResult.getDataMap();
    }

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("根据Id查询角色")
    @PostMapping("/selectRoleById")
    @ApiImplicitParam(name = "id", value = "角色Id", required = true)
    public Map selectRoleById(Long id){
        Role role = roleService.selectRole(id);
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRid(id);
        List<RolePermission> select = rolePermissionService.select(rolePermission);
        List<Permission> permissions = new ArrayList<>();
        for (RolePermission rolePermission1:select){
            Permission permission = permissionService.selectPermissionById(rolePermission1.getPid());
            permissions.add(permission);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("根据Id查询角色");
        jsonResult.setData(role);
        jsonResult.put("permission",permissions);
        List<Permission> maps = permissionService.selectAllPermission();
        jsonResult.put("allPermission",maps);
        return jsonResult.getDataMap();
    }

    @ApiOperation("更新角色")
    @PostMapping("/updateRoleById")
    @ApiImplicitParam(name = "rolePermissionVo", value = "角色信息", required = true)
    public Map updateRoleById(@RequestBody RolePermissionVo rolePermissionVo){
        Role role = roleService.selectRole(rolePermissionVo.getId());
        role.setName(rolePermissionVo.getName());
        role.setDesc(rolePermissionVo.getDesc());
        roleService.updateRole(role);

        roleService.deleteById(role.getId());
        roleService.delectByRid(rolePermissionVo.getId());

        List<Integer> oids= rolePermissionVo.getOids();
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRid(role.getId());
        for (int i=0;i<oids.size();i++){
            rolePermission.setPid(Long.parseLong(oids.get(i).toString()));
            rolePermissionService.insertPermission(rolePermission);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("更新角色");
        return jsonResult.getDataMap();
    }

    @ApiOperation("添加角色")
    @PostMapping("/addRole")
    @ApiImplicitParam(name = "rolePermissionVo", value = "管理员Id", required = true)
    public Map addRole(@RequestBody RolePermissionVo rolePermissionVo){
        Role role = new Role();
        /*role.setName(map.get("name").toString());
        role.setDesc(map.get("desc").toString());
        role.setCreateTime(new Timestamp(System.currentTimeMillis()));
        Role insert = roleService.insert(role);
        List<Integer> oids= (List)map.get(Long.parseLong(map.get("rid").toString()));*/

        role.setName(rolePermissionVo.getName());
        role.setDesc(rolePermissionVo.getDesc());
        role.setCreateTime(new Timestamp(System.currentTimeMillis()));

        roleService.insertRole(role);
        Role insert = roleService.selectByRole(role);

        List<Integer> oids = rolePermissionVo.getOids();

        RolePermission rolePermission = new RolePermission();
        rolePermission.setRid(insert.getId());
        for (int i=0;i<oids.size();i++){
            rolePermission.setPid(Long.parseLong(oids.get(i).toString()));
            rolePermissionService.insertPermission(rolePermission);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加角色成功");
        return jsonResult.getDataMap();
    }
    @Autowired
    private AdminService adminService;
    @PostMapping("/test")
    @ApiOperation("全部权限查询")
    public Map test(Long id){
        List<Permission> permissionList = adminService.getPermissionList(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(permissionList);
        jsonResult.setMsg("全部权限查询");
        return jsonResult.getDataMap();
    }
}
